package com.example.zg.zg_312_ordering_new.model;

import com.example.zg.zg_312_ordering_new.beans.ChangeBean;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.example.zg.zg_312_ordering_new.listener.ChangeListener;
import com.example.zg.zg_312_ordering_new.listener.Changeiface;
import com.example.zg.zg_312_ordering_new.services.UserinfoService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChangeModel implements Changeiface {
    private Retrofit retrofit;

    public ChangeModel(){
        retrofit=new Retrofit
                .Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constants.BASE_URL)
                .build();
    }

    @Override
    public void updateUserById(int user_id, String username, String userpass, String mobilenum, String address, final ChangeListener modifyListener) {
        UserinfoService userinfoService=retrofit.create(UserinfoService.class);//创建网络接口
        Call<ChangeBean> call=userinfoService.updateUserById(user_id, username, userpass, mobilenum, address);
        //发送网络请求
        call.enqueue(new Callback<ChangeBean>() {
            @Override
            public void onResponse(Call<ChangeBean> call, Response<ChangeBean> response) {
                if (response.isSuccessful()&&response!=null){
                    modifyListener.onResponse(response.body());
                }
                else modifyListener.onFail("");
            }

            @Override
            public void onFailure(Call<ChangeBean> call, Throwable t) {
                modifyListener.onFail(t.toString());
            }
        });
    }
}
